package com.xz.group12306.business.service.Impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.xz.group12306.business.dao.BaseDao;
import com.xz.group12306.business.dao.UserDao;
import com.xz.group12306.business.dao.Impl.UserDaoImpl;
import com.xz.group12306.business.service.UserService;
import com.xz.group12306.domain.Cart;
import com.xz.group12306.domain.User;
import com.xz.group12306.utils.ServiceException;

public class UserServiceImpl implements UserService{

	@Override
	public User login(String username, String password, String rule) {
		Connection conn = null;
		conn=BaseDao.getConnection();
		BaseDao.beginTransaction(conn);
		// TODO Auto-generated method stub
		UserDaoImpl userDaoImpl = new UserDaoImpl(conn);// 实例化UserDao的实现类
		User user=userDaoImpl.selectUser(username,password, rule);
		BaseDao.commit(conn);
		BaseDao.closeConnection(conn);
		return user;		
	}
	
	//添加用户连接到数据库，调用dao包里的方法
			public void addUser(User user) {
				Connection conn = null;
				try {
					conn = BaseDao.getConnection();
					UserDaoImpl userDaoImpl=new UserDaoImpl(conn);
					try {
						BaseDao.beginTransaction(conn);
				
						userDaoImpl.save(user);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					BaseDao.commit(conn);
				} finally {
					BaseDao.closeConnection(conn);
				}
			}

			
			//查询所有的用户
			@Override
			public User getAllUser(User one) {
				// TODO Auto-generated method stub
				User user=null;
				Connection conn=null;
				conn=BaseDao.getConnection();
				UserDaoImpl userDaoImpl=new UserDaoImpl(conn);
				BaseDao.beginTransaction(conn);
				try {
					user=userDaoImpl.allUser(one);
					BaseDao.commit(conn);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					BaseDao.rollback(conn);
					throw new ServiceException("查询错误", e);
				}finally {
					BaseDao.closeConnection(conn);
				}
				return user;
			}

			@Override
			public void updatePassword(String password, int id) {
				// TODO Auto-generated method stub
				Connection conn = null;
				conn=BaseDao.getConnection();
				BaseDao.beginTransaction(conn);
				// TODO Auto-generated method stub
				UserDaoImpl userDaoImpl = new UserDaoImpl(conn);// 实例化UserDao的实现类
				userDaoImpl.updatePwd(password, id);
				BaseDao.commit(conn);
				BaseDao.closeConnection(conn);
			}

			@Override
			public List<Cart> findCartByUser(User user, int page, int rows) {
				// TODO Auto-generated method stub
				Connection conn = null;
				List<Cart> carts = null;
				try {
				conn = BaseDao.getConnection();
				UserDao userDao = new UserDaoImpl(conn);
				BaseDao.beginTransaction(conn);
				carts = userDao.findCartByUser(user, page, rows);
				BaseDao.commit(conn);
				} catch (SQLException e) {
				BaseDao.rollback(conn);
				try {
				throw new ServiceException("查询错误", e);
				} catch (ServiceException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				}
				} finally {
				BaseDao.closeConnection(conn);
				}

				return carts;
			}

			@Override
			public void updateUser(User user) {
				// TODO Auto-generated method stub
				Connection conn=null;
				try {
					
				
				conn=BaseDao.getConnection();
				UserDao userDao=new UserDaoImpl(conn);
				BaseDao.beginTransaction(conn);
				userDao.updateUser(user);
				BaseDao.commit(conn);
				} catch (Exception e) {
					// TODO: handle exception
					BaseDao.rollback(conn);
				}finally {
					BaseDao.closeConnection(conn);
				}
			}

}
